<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php';
require_once 'CMS/User.php';
//Người tạo : Nguyễn Hồng Danh - Team 2
//Module 	: DescriptionNews
//Chức năng	: Show ra mô tả của tin tức
 
class ProductDetail_IndexController {

	public function init(){
		parent::init();
	}

	public function indexAction(){
		//Start------Check Input Parameter
		$path = APPLICATION_PATH."/widgets/productdetail/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		//End--------Check Input Parameter
		
		/************ Start check user login *********/
		$session1 = new Zend_Session_Namespace('login.auth');
		if(isset($session1->id_user))
		{
			$this->view->login = "1";
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_content($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		/************ End check user login *********/
		
		
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/productdetail');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
		$this->view->productinfo = $language->get('text_product_info');
		$this->view->confirm = $language->get('confirm');
		$this->view->lag 	= $language->get('lag');
		$this->view->more 	= $language->get('text_more');
		$this->view->textPrice 	= $language->get('text_Price');
		$this->view->addCart 	= $language->get('text_addCart');
		/*========== End Text ==============*/
		
		//-----------------------Khai báo các parameter----------------------
		$tbThings= new ProductDetail_Model_Things();
		$tbLike= new ProductDetail_Model_Like();
		$tbImage= new ProductDetail_Model_Imagetothing();		
		$dateInt = new Util();
		$access=false;
		//---------------------- Khai báo các parameter----------------------//
		
		
		//-----------------------Check Input Parameter-----------------------
		if(isset($_GET["id"]) && $dateInt->is_interger($_GET["id"]))
		{
			//Check thing_id có trong db?
			$temp = $tbThings->getThingById($_GET["id"]);
			
			$ProductDetail = $temp[0];
				
			$desc= unserialize($ProductDetail['short_description']);
			$common_info = unserialize($ProductDetail['common_info']);
			
			//to test
			//if($common_info=="" or $common_info==null)
			//{
				//print_r(htmlentities($ProductDetail['common_info']));
				//$ProductDetail['common_info']=str_replace("\\\\&quot;", "\"", $ProductDetail['common_info']);
				//print_r("<br><br>{".$common_info."}");
			//}
			//print_r($ProductDetail['common_info']);
			//die;
			
			if(isset($desc[$name['name']]))
				$ProductDetail['short_description'] = $desc[$name['name']];
			else 
				$ProductDetail['short_description'] = $desc['Default'];
			if(isset($common_info[$name['name']]))
				$ProductDetail['common_info'] = $common_info[$name['name']];
			else 
				$ProductDetail['common_info'] = $common_info['Default'];
								
			if(count($ProductDetail)>0)
				$access=true;
		}
		if(!$access) 
		{
			$this->_redirect('/index/');
		}
		//-----------------------Check Input Parameter------------------------//
		
		//Đếm số người thích tin này
		$CountLike=count($tbLike->getLikeByThingsId($_GET["id"]));
		
		//Lấy tất cả image của sp
		$AllImages=$tbImage->getImageByThingsId($_GET["id"]);
		$no=0;
		$AllImage=array();
		if(count($AllImages)>0)
		{
			foreach($AllImages as $image)
			{
				$AllImage[$no]=array('url'=>$image['url']);
				$no++;
			}
		}
		
		if(count($ProductDetail)>0)
		{
			$AllImage[$no]=array('url'=>$ProductDetail['avatar']);
		}
			
		//Lấy ngày đăng tin
		$Update_Date=$dateInt->makeDays($ProductDetail['update_date']); 
		
		//-----------------------Start update view of things------------------------
		$ProductDetail['views']=$ProductDetail['views']+1;
		$tbThings->updateViewThing($_GET["id"],$ProductDetail['views']);
		//-----------------------end update view of things------------------------//
		
		//---------------------Truyền parameter qua View-----------------------
		$this->view->Update_Date=$Update_Date;				
		$this->view->ProductDetail =$ProductDetail;	
		$this->view->AllImage=$AllImage;	
		$this->view->CountLike = $CountLike;
		//---------------------Truyền parameter qua View-----------------------//	
		
	}	
	
	function unhtmlspecialchars($str) {
   $str = str_replace("&amp;", "&", $str);
   $str = str_replace("&apos", "'", $str);
   $str = str_replace("&#039;", "'", $str);
   $str = str_replace('&quot;', "\"", $str);
   $str = str_replace('&lt;', "<", $str);
   $str = str_replace('&gt;', ">", $str);
   return $str;
}

}